package com.noahplat.device.controller;

import com.noahplat.common.utils.Result;
import com.noahplat.device.pojo.Model;
import com.noahplat.device.service.ModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

/**
 * @ClassName: ModelController
 * @Description: 设备型号ID
 * @Author 李景星
 * @Date 2021/3/9
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/model")
@Validated
public class ModelController {


    @Autowired
    private ModelService modelService;

    /**
     * 获取所有设备型号
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-device:ModelController:getModel')")
    @GetMapping("/getModel")
    public Result getModel(){
        return modelService.getModel();
    }

    /**
     * 通过ID查询设备型号
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-device:ModelController:getById')")
    @GetMapping("/getById")
    public Result getById(@NotNull(message = "型号ID不能为空 ") @Min(value = 1, message = "型号Id不能为空")Long id){
        return modelService.getById(id);
    }

    /**
     * 添加设备型号
     */
    @PreAuthorize("hasAuthority('noahplat-service-device:ModelController:addModel')")
    @PostMapping("/addModel")
    public Result addModel(@RequestBody Model model){
        return modelService.addModel(model);
    }

    /**
     * 修改设备型号
     * @param model
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-device:ModelController:updateModel')")
    @PutMapping("/updateModel")
    public Result updateModel(@RequestBody Model model){
        return modelService.updateModel(model);
    }

    /**
     * 分页获取设备所有型号
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-device:ModelController:selectModel')")
    @GetMapping("/selectModel")
    public Result selectModel(@RequestParam(defaultValue = "1")Integer page,@RequestParam(defaultValue = "10")Integer limit){
        return modelService.selectModel(page, limit);
    }

    /**
     * 删除设备型号
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-device:ModelController:delModel')")
    @DeleteMapping("/delModel")
    public Result delModel(@NotNull(message = "型号ID不能为空 ") @Min(value = 1, message = "型号Id不能为空")Long id){
        return modelService.delModel(id);
    }
}
